// --------------------------------------------------------------------
//   name 	: 
//   desc 	: 
//   version 	: 
//   dev 	: 
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// --------------------------------------------------------------------
	   plugins.traveltimes = { // calculates traveltimes on the map

   'enhance_game_map': function() {
      $("#info_content #info_extra_info").after("<tr><td colspan=\"2\" id=\"info_traveltimes\"></td></tr>");
      var old_map_popup = unsafeWindow.map_popup;
      setFunc("map_popup", function map_popup(title, bonus_image, bonus_text, points, owner, ally, village_groups, moral, village_id, source_id) {

      old_map_popup(title, bonus_image, bonus_text, points, owner, ally, village_groups, moral, village_id, source_id);

      window.setTimeout(function() {
         var TW_Image_Base = personals.getImageBase();
         var Target_Coords = title.match(/\((-?\d+)\|(-?\d+)\)/);
         var Current_Coords = (xpathGetFirst("/html/head/title").innerHTML).match(/.*\((-?\d+)\|(-?\d+)\)/);
         html = '<tr><td colspan="2"><table style="border:1px solid #DED3B9" cellpadding="0" cellspacing="0"><tr class="center">';

         UnitIcons = new Array("spear","sword","axe","archer","spy","light","marcher","heavy","ram","catapult","knight","snob");

		 var UnitTravelTimes = plugins.ServerSettings.UnitSpeeds();
		 var WorldInfo = plugins.ServerSettings.isPaladin(); // returns "true" if the world allows paladins, "false" if world doesnt allow paladins. [paladins/archers/horse archers]

         if (UnitTravelTimes) {
            var distance = Math.sqrt(Math.pow(Math.abs(Current_Coords[1] - Target_Coords[1]),2) + Math.pow(Math.abs(Current_Coords[2] - Target_Coords[2]),2));
            if (distance > 0) {
               var j = 0;
               for (i=0;i<12;i++) {
                  if ( (WorldInfo.toString=="false") && (i == 3 || i == 6 || i == 10)) { continue; }
                  var bg = (j%2 == 0) ? 'F8F4E8' : 'DED3B9';
                  j++;
                  html += '<td style="padding:2px;font-size: 9px;background-color:#'+bg+'">';
                  html += '<img src="' + TW_Image_Base + 'unit/unit_' + UnitIcons[i] + '.png"/>';
                  var time = Math.round(UnitTravelTimes[i] * distance * 60);
                  html += Math.floor(time/3600) + ":" + (((t=Math.floor(time%3600/60)) < 10) ? '0' + t : t) + ":" + (((t=Math.floor(time%60)) < 10) ? '0' + t : t);
                  html += '</td>';
               }
               html += "</tr><tr>"+plugins.quickattack.get_overview_row(twe.curVillage);
               $("#info_traveltimes").html(html);
            } else {
               $("#info_traveltimes").html("");
            }
         } else {
            $("#info_traveltimes").html("Error with ServerSettings Plugin. Please report on forum.");
         }

      }, 0);

   });
   },

   'info' : {
      'mandatory' : false,
      'name' : "Traveltimes",
      'desc' : "Adds traveltimes to the world map",
      'version' : "0.2",
      'dev' : "JohnDoe"
     }
   }; 
